package com.neuedu.web.service;

import com.neuedu.web.common.PageInfo;
import com.neuedu.web.dao.RoleDao;
import com.neuedu.web.dao.RoleMenuDao;
import com.neuedu.web.dao.UserRoleDao;
import com.neuedu.web.po.RoleMenuPO;
import com.neuedu.web.po.RolePO;
import com.neuedu.web.po.UserRolePO;
import com.neuedu.web.utils.BeanConvertUtils;
import com.neuedu.web.utils.ParamUtils;
import com.neuedu.web.vo.RoleVO;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 金山
 * 项目：javaweb
 * site: https://blog.fulfill.com.cn
 * 描述  Dept 业务工呢
 * @data 2024年11月21日14:23:14
 */
public class RoleService {

    private RoleDao roleDao = new RoleDao();
    private UserRoleDao userRoleDao = new UserRoleDao();
    private RoleMenuDao roleMenuDao = new RoleMenuDao();


    public List list(HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from ums_role";
        List<RolePO> list = roleDao.selectAll(sql);

        return BeanConvertUtils.po2Vo(list, RoleVO.class);

    }



    public Map listRoleByUserId(HttpServletRequest request, HttpServletResponse response) throws IOException, NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {

        String userid = request.getParameter("userid");



        String sql = " select * from ums_role";
        List<RolePO> list = roleDao.selectAll(sql);

        String sql2 = "SELECT * FROM user_role WHERE userid =  ?  ";
        List<UserRolePO> userRoleList = userRoleDao.selectAll(UserRolePO.class, sql2, userid);

        Map map = new HashMap();
        map.put("roleList",BeanConvertUtils.po2Vo(list, RoleVO.class));//所有角色列表
        map.put("userRoleList",userRoleList);//当前用户已授权的角色



        return map;

    }


    public PageInfo<RoleVO> page(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {


        String sql = " select * from ums_role";


        PageInfo pageInfo = ParamUtils.getPageInfo(request);
        List<RolePO> dataList = roleDao.selectPage(pageInfo.getCurrent(), pageInfo.getSize(), sql);
        int total = roleDao.total(sql);

        pageInfo.setTotal(total);

        // 数据
        pageInfo.setData(BeanConvertUtils.po2Vo(dataList, RoleVO.class));

        return pageInfo;

    }

    /**
     * 保存
     * @param request
     * @param response
     * @return
     */
    public boolean save(HttpServletRequest request, HttpServletResponse response) {
        RolePO data = ParamUtils.getParam(RolePO.class, request);
        int count = 0;
        if(data.getRoleId() != null){
            count =   roleDao.update(data);
        }else{
            count =  roleDao.insert(data);
        }



        //处理  角色对应的菜单    role_menu
        roleMenuDao.deleteByRoleId(data.getRoleId());

        //所有 菜单和角色插入一次
        if(data.getMenudIds() != null && data.getMenudIds().length()>0){
            String[] menuIds = data.getMenudIds().split(",");

            for (String menuId : menuIds) {
                RoleMenuPO roleMenuPO = new RoleMenuPO(Long.parseLong(menuId),data.getRoleId());
                roleMenuDao.insert(roleMenuPO);
            }
        }


        return count > 0;
    }

    public boolean delete(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");

        return roleDao.delete(Long.parseLong(id)) >0;
    }


    public RoleVO getById(HttpServletRequest request, HttpServletResponse response) throws NoSuchFieldException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        String id = request.getParameter("id");

        RoleVO vo = BeanConvertUtils.conver(RoleVO.class, roleDao.selectById(Long.parseLong(id)));

        //查询 角色拥有的菜单
        List<RoleMenuPO> roleMenuPO = roleMenuDao.selectAll("select * from role_menu where role_id = ?", id);
        StringBuffer menuIdS = new StringBuffer();

        for (RoleMenuPO menuPO : roleMenuPO) {

            menuIdS.append(menuPO.getMenuId()+",");   // 1,    2,  3,
        }
        if(menuIdS.length() >0){
            menuIdS.deleteCharAt(menuIdS.length()-1);
        }

        vo.setMenudIds(menuIdS.toString());  //   1,2,3
        return vo;
    }
}
